package cn.bmbanjia.zebra2024.mybatisflex;


import cn.bmbanjia.zebra2024.apps.clewAndroid.satoken.ClewAndroidCurrentUser;
import cn.bmbanjia.zebra2024.apps.clewAndroid.satoken.StpClewAndroidUtil;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.satoken.LaddieAndroidCurrentUser;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.satoken.StpLaddieAndroidUtil;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.satoken.MeituanCurrentUser;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.satoken.StpMeituanUtil;
import cn.bmbanjia.zebra2024.frame.aop.WebLogAspect;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.annotation.UpdateListener;

public class InsertOrUpdateListener implements UpdateListener, InsertListener {

    @Override
    public void onInsert(Object entity) {
        if (entity instanceof BaseEntity be) {
            if (ObjUtil.isNull(be.getCreateUserId())) {
                String URI = WebLogAspect.requestURIThread.get();
                if (StrUtil.isNotBlank(URI)) {
                    if (URI.startsWith("/meituanMiniApp") && StpMeituanUtil.isLogin()) {
                        MeituanCurrentUser meituanCurrentUser = StpMeituanUtil.currentUser();
                        be.setCreateUserId(meituanCurrentUser.getUserId());
                        be.setCreateUserName(meituanCurrentUser.getUserNickName());
                    } else if (URI.startsWith("/laddieAndroid") && StpLaddieAndroidUtil.isLogin()) {
                        LaddieAndroidCurrentUser laddieAndroidCurrentUser = StpLaddieAndroidUtil.currentUser();
                        be.setCreateUserId(laddieAndroidCurrentUser.getLaddieId());
                        be.setCreateUserName(laddieAndroidCurrentUser.getLaddieName());
                    } else if (URI.startsWith("/clewAndroid") && StpClewAndroidUtil.isLogin()) {
                        ClewAndroidCurrentUser currentUser = StpClewAndroidUtil.currentUser();
                        be.setCreateUserId(currentUser.getUserId());
                        be.setCreateUserName(currentUser.getUserName());
                    }
                }
            }
        }
    }

    @Override
    public void onUpdate(Object entity) {
        if (entity instanceof BaseEntity be) {
            if (ObjUtil.isNull(be.getUpdateUserId())) {
                String URI = WebLogAspect.requestURIThread.get();
                if (StrUtil.isNotBlank(URI)) {
                    if (URI.startsWith("/meituanMiniApp") && StpMeituanUtil.isLogin()) {
                        MeituanCurrentUser meituanCurrentUser = StpMeituanUtil.currentUser();
                        be.setUpdateUserId(meituanCurrentUser.getUserId());
                        be.setUpdateUserName(meituanCurrentUser.getUserNickName());
                    } else if (URI.startsWith("/laddieAndroid") && StpLaddieAndroidUtil.isLogin()) {
                        LaddieAndroidCurrentUser laddieAndroidCurrentUser = StpLaddieAndroidUtil.currentUser();
                        be.setUpdateUserId(laddieAndroidCurrentUser.getLaddieId());
                        be.setUpdateUserName(laddieAndroidCurrentUser.getLaddieName());
                    } else if (URI.startsWith("/clewAndroid") && StpClewAndroidUtil.isLogin()) {
                        ClewAndroidCurrentUser currentUser = StpClewAndroidUtil.currentUser();
                        be.setUpdateUserId(currentUser.getUserId());
                        be.setUpdateUserName(currentUser.getUserName());
                    }
                }
            }
        }
    }

}
